<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">

<!--版本配置模态框-->
<div id="modal-version-config" class="modal fade" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" style="width:800px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">版本配置</h4>
            </div>
            <div class="modal-body">
                <table id="bootstrap-table-version-config"></table>
            </div>
        </div>
    </div>
</div>
<!--创建机加定额模态框-->
<div id="modal-create-macQuotaConfig" class="modal fade" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" style="width:1000px;">
        <div class="modal-content" style="height:800px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">创建机加定额</h4>
            </div>
            <div class="modal-body">
                <form id="createMacQuotaConfigForm" class="form-horizontal">
                    <div clsss="box">
                        <div class="box-head">
                            <div class="row">
                                <div class="form-inline" style="padding: 10px">
                                    <div class="form-group" style="margin-left:5px;margin-right: 50px;">
                                        <lable class="control-label">版本号：</lable>
                                        <lable class="control-label">
                                            <input name="version" class="form-control" type="text" maxlength="255"/>
                                        </lable>
                                    </div>
                                    <div class="form-group">
                                        <lable class="control-label">是否默认：</lable>
                                        <lable class="control-label">
                                            <select name="isDefault" class="form-control">
                                                <option value="0" selected="selected">否</option>
                                                <option value="1">是</option>
                                            </select>
                                        </lable>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="box-body">
                            <table id="bootstrap-table-new-machiningQuotaConfig" class="table bootstrap-table"
                                   data-height="500"></table>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer" style="margin-top:50px;">
                <button type="button" shiro:haspermission="basic:macquota:view" class="btn btn-info"
                        id="btn-create-macQuotaConfigList">提交
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<!--修改定额模态框模态框-->
<div id="modal-create-dict" class="modal fade" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog" style="width:800px">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">修改定额</h4>
            </div>
            <div class="modal-body">
                <form id="dictForm" class="form-horizontal">
                    <input type="hidden" id="id" name="id">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设备型号</label>
                        <div class="col-sm-6">
                            <span id="name"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">设备类型</label>
                        <div class="col-sm-6">
                            <span id="nameType"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">工序</label>
                        <div class="col-sm-6">
                            <span id="workingProcedure"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label">版本号</label>
                        <div class="col-sm-6">
                            <span id="version"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-4 control-label" for="dictForm-unit">定额</label>
                        <div class="col-sm-6">
                            <input id="dictForm-unit" type="text" class="form-control" oninput="formatNumber(this)"
                                   name="price">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" shiro:haspermission="basic:macquota:view" class="btn btn-info"
                        id="btn-create-dict">提交
                </button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>

<section class="content-header">
    <h1>机加定额配置</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label for="query-name">设备型号</label>
                                    <input id="query-name" type="text" name="name" class="form-control"
                                           placeholder="请输入设备型号">
                                </div>
                                <div class="form-group">
                                    <label for="query-nameType">设备类型</label>
                                    <select id="query-nameType" type="text" name="nameType"
                                            class="form-control select">
                                    </select>
                                </div>
                                <!--                                <div class="form-group">-->
                                <!--                                    <label for="query-workingProcedure">工序</label>-->
                                <!--                                    <select id="query-workingProcedure" type="text" name="workingProcedure"-->
                                <!--                                            class="form-control select">-->
                                <!--                                    </select>-->
                                <!--                                </div>-->
                                <div class="form-group">
                                    <label for="query-version">版本号</label>
                                    <select id="query-version" type="text" name="version"
                                            class="form-control select">
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="query-isDefault">是否系统默认</label>
                                    <select id="query-isDefault" type="text" name="isDefault"
                                            class="form-control select">
                                        <option value="">请选择</option>
                                        <option value="1">是</option>
                                        <option value="2">否</option>
                                    </select>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search"
                                        onclick="js.table.search(table)">
                                    <span class="Bold">搜索</span>
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
                <div id="toolBar">
                    <button shiro:hasPermission="basic:macquota:view" id="btn-create-macQuotaConfig" type="button"
                            class="btn btn-success">创建机加定额
                    </button>
                    <button shiro:hasPermission="basic:macquota:view" id="btn-config-version" type="button"
                            class="btn btn-warning">配置版本号
                    </button>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    let table, versionTable;


    /** 打开编辑数据模态框 */
    function openEditDictModal(id) {
        debugger
        let rowData = $('#bootstrap-table').bootstrapTable('getRowByUniqueId', id);
        $("#dictForm").fillData(rowData);
        $("#dictForm").fillSpan(rowData);
        js.modal.open("modal-create-dict");
        setTimeout(function () {
            js.validate.validing("dictForm");
        }, 500);
    }

    $(function () {
        loadSearchConditionData();
        /** 加载列表 */
        table = js.table.init({
            url: ctx + "basicInfoConfig/macQuotaConfig/list",
            showExport: false,
            uniqueId: 'id',
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "设备型号", field: 'name'},
                {title: "设备类型", field: 'nameType'},
                // {title: "工序", field: 'workingProcedure'},
                {title: "定额", field: 'price'},
                {title: "版本号", field: 'version'},
                {
                    title: "是否系统默认", field: 'isDefault',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<i class="glyphicon glyphicon-ok" style="color:#00A65A"></i>';
                        } else if (value === 0) {
                            return '<i class="glyphicon glyphicon-remove" style="color:#EB3941"></i>';
                        }
                    }
                },
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row) {
                        let actions = [];
                        actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="openEditDictModal(' + row.id + ')"> 编辑定额</a> ');
                        return actions.join('');
                    }
                }
            ]
        });

        /** 初始化验证 */
        js.validate.init("dictForm", {
            fields: {
                price: {
                    validators: {
                        notEmpty: {message: '请输入定额'}
                    }
                }
            }
        });

        /**
         * 修改定额
         */
        $("#btn-create-dict").click(function () {
            js.validSubmit({
                formId: "dictForm",
                url: ctx + "basicInfoConfig/macQuotaConfig/updatePrice",
                data: new FormData($("#dictForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                        js.modal.hide("modal-create-dict");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })

        /** 隐藏 modal-create-dict */
        js.modal.hideEvent("modal-create-dict", function () {
            js.validate.reset("dictForm");
            js.reset("dictForm");
        });

        /** 隐藏 modal-create-macQuotaConfig */
        js.modal.hideEvent("modal-create-macQuotaConfig", function () {
            js.reset("createMacQuotaConfigForm");
        });

        /**
         * 打开版本控制模态框
         */
        $("#btn-config-version").on('click', function () {
            loadVersionTable();
            js.modal.open("modal-version-config");
        });
        /**
         * 打开创建机加定额模态框
         */
        $("#btn-create-macQuotaConfig").on('click', function () {
            loadNewMacQuotaConfigTable();
            js.modal.open("modal-create-macQuotaConfig");
        });
        /**
         * 创建新版本机加定额
         */
        $("#btn-create-macQuotaConfigList").on('click', function () {
            let isPass = true;
            let version = $("#createMacQuotaConfigForm [name='version']").val();
            if (version.trim().length === 0) {
                js.modal.warning("请输入版本号！");
                return false;
            } else {
                js.post({
                    url: ctx + "basicInfoConfig/macQuotaConfig/checkExist",
                    data: {
                        version: $("#createMacQuotaConfigForm [name='version']").val()
                    },
                    sync: false,
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.warning(result.msg);
                            isPass = false;
                            return;
                        } else {
                            $("#createMacQuotaConfigForm [name='price']").each(function (index) {
                                if ($(this).val() === '') {
                                    js.modal.warning("第" + eval(index + 1) + "行的定额没有填写！");
                                    isPass = false;
                                    return false;
                                }
                            });
                            if (!isPass) {
                                return;
                            } else {
                                let dataArray = new Array();
                                $("#bootstrap-table-new-machiningQuotaConfig tr").each(function (index) {
                                    let objData = {
                                        name: $(this).find("[name='name']").val(),
                                        nameType: $(this).find("[name='nameType']").val(),
                                        price: $(this).find("[name='price']").val()
                                    };
                                    dataArray.push(objData);
                                });
                                dataArray.splice(0, 1);
                                js.post({
                                    url: ctx + "basicInfoConfig/macQuotaConfig/createMacQuotaConfigList",
                                    data: {
                                        version: $("#createMacQuotaConfigForm [name='version']").val(),
                                        isDefault: $("#createMacQuotaConfigForm [name='isDefault']").val(),
                                        machiningData: JSON.stringify(dataArray)

                                    },
                                    success: function (result) {
                                        if (result.type === web_status.SUCCESS) {
                                            js.modal.success(result.msg);
                                            js.table.refresh(table);
                                            js.modal.hide('modal-create-macQuotaConfig');
                                        } else {
                                            s.modal.warning(result.msg);
                                        }
                                        js.modal.closeLoading();
                                    },
                                    error: function () {
                                        s.modal.warning(result.msg);
                                        js.modal.closeLoading();
                                    }
                                });
                            }
                        }
                        js.modal.closeLoading();
                    },
                    error: function () {
                        s.modal.warning(result.msg);
                        js.modal.closeLoading();
                    }
                });
            }


        });
    });

    /**
     * 加载搜索条件数据
     */
    function loadSearchConditionData() {
        js.post({
            url: ctx + "basicInfoConfig/macQuotaConfig/loadSearchConditionData",
            data: {},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    let nameTypes = result.data.nameTypeList;
                    $("#query-nameType").empty();
                    $("#query-nameType").append('<option value="">全部</option>');
                    for (let i = 0; i < nameTypes.length; i++) {
                        $("#query-nameType").append('<option value="' + nameTypes[i].name + '">' + nameTypes[i].name + '</option>');
                    }
                    let versions = result.data.versionList;
                    $("#query-version").empty();
                    $("#query-version").append('<option value="">全部</option>');
                    for (let i = 0; i < versions.length; i++) {
                        $("#query-version").append('<option value="' + versions[i].version + '">' + versions[i].version + '</option>');
                    }
                }
            }
        });
    }

    /**
     * 加载版本列表数据
     */
    function loadVersionTable() {
        $("#bootstrap-table-version-config").bootstrapTable('destroy');
        versionTable = js.table.init({
            id: "bootstrap-table-version-config",
            url: ctx + "basicInfoConfig/macQuotaConfig/versionList",
            pageSize: 10,
            showExport: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "版本号", field: 'version'},
                {
                    title: "是否系统默认", field: 'isDefault',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<i class="glyphicon glyphicon-ok" style="color:#00A65A"></i>';
                        } else if (value === 0) {
                            return '<i class="glyphicon glyphicon-remove" style="color:#EB3941"></i>';
                        }
                    }
                },
                {
                    title: "操作",
                    width: '250',
                    formatter: function (value, row) {
                        let actions = [];
                        if (hasP('basic:macquota:view')) {
                            if (row.isDefault === 0) {
                                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="setDefault(' + row.id + ')">设置成系统默认</a> ');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });
    }

    /**
     * 加载新创建的机加定额
     */
    function loadNewMacQuotaConfigTable() {
        js.post({
            url: ctx + "basicInfoConfig/macQuotaConfig/getNewMachiningQuotaConfigList",
            data: {},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    $("#bootstrap-table-new-machiningQuotaConfig").bootstrapTable("destroy");
                    let data = result.data.rows;
                    if (data.length > 0) {
                        // $("#bootstrap-table-new-machiningQuotaConfig").append('<thead><tr><th>序号</th><th>设备型号</th><th>设备类型</th><th>设备定额</th></tr></thead><tbody></tbody>');
                        // for (let i = 0; i < data.length; i++) {
                        //     $("#bootstrap-table-new-machiningQuotaConfig tbody").append('<tr>' +
                        //         '<td>' + eval(i + 1) + '</td>' +
                        //         '<td><input style="border:none;outline: none;" name="name" type="text" readonly="readonly" value="' + data[i].name + '" /></td>' +
                        //         '<td><input style="border:none;outline: none;" name="nameType" type="text" readonly = ring-0 value="' + data[i].nameType + '" /></td>' +
                        //         '<td><input name="price" type="text" oninput="formatNumber(this)" maxlength="9" /></td>' +
                        //         '</tr>');
                        // }
                        js.table.init({
                            id: "bootstrap-table-new-machiningQuotaConfig",
                            data: data,
                            showExport: false,
                            pagination: false,
                            columns: [
                                {
                                    title: '序号', field: 'id', width: '50',
                                    formatter: function (value, row, index) {
                                        return index + 1;
                                    }
                                },
                                {
                                    title: "设备型号", field: 'name',
                                    formatter: function (value, row) {
                                        return '<input style="border:none;outline: none;" name="name" type="text" readonly="readonly" value="' + value + '" />';
                                    }
                                },
                                {
                                    title: "设备类型", field: 'nameType',
                                    formatter: function (value, row) {
                                        return '<input style="border:none;outline: none;" name="nameType" type="text" readonly = ring-0 value="' + value + '" />';
                                    }
                                },
                                {
                                    title: "定额", field: 'price',
                                    formatter: function (value, row) {
                                        return '<input name="price" type="text" oninput="formatNumber(this)" maxlength="9" value="1" />';
                                    }
                                }
                            ]
                        });
                    }

                }
            }
        });

    }

    /**
     * 设置这个个版本号为默认
     * @param id
     */
    function setDefault(id) {
        js.confirmPost({
            content: "尊敬的用户，是否把此项设置成系统默认？",
            url: ctx + "basicInfoConfig/macQuotaConfig/setDefault",
            data: {
                id: id
            },
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    js.table.refresh(versionTable);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }


    /**
     * 验证输入有效的整数和四位小数
     */
    function formatNumber(obj) {
        obj.value = obj.value.replace(/[^-\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(/\-{2,}-/g, "-"); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
        //如果上去就写- 自动替换
        if (obj.value.indexOf("-") >= 0) {
            obj.value = obj.value.replace('-', "");
        }
        //如果上去就写.   自动补个0   形成0.
        if (obj.value.indexOf(".") == 0) {
            obj.value = "0" + obj.value;
        } else if (obj.value.indexOf(".") > 1) {
            //如果输入01.3自动变成1.3，自动去除整数部分的0
            if (obj.value.indexOf("0") == 0) {
                obj.value = obj.value.substr(1, obj.value.length - 1);
            }
        }
        //如果输入023直接变成23;自动去除整数部分的0
        if (obj.value.length > 2 && obj.value.indexOf(".") < 0) {
            if (obj.value.indexOf("0") == 0) {
                obj.value = obj.value.substr(1, obj.value.length - 1);
            }
        }
    }
</script>
</body>
</html>
